package com.xydtech.ecard.ecard.service.impl;

import com.xydtech.common.utils.AESEncryptionUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.ecard.domain.EcardApiPersonArea;
import com.xydtech.ecard.ecard.domain.EcardApiPersonAreaHandover;
import com.xydtech.ecard.ecard.mapper.EcardApiPersonAreaHandoverMapper;
import com.xydtech.ecard.ecard.mapper.EcardApiPersonAreaMapper;
import com.xydtech.ecard.ecard.service.IEcardApiPersonAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 电子名片个人网格区域信息Service业务层处理
 *
 * @author yuzhiguo
 * @date 2024-08-27 上午9:37
 */
@Service
public class EcardApiPersonAreaServiceImpl implements IEcardApiPersonAreaService {

    @Autowired
    private EcardApiPersonAreaMapper ecardPersonAreaMapper;
    @Autowired
    private EcardApiPersonAreaHandoverMapper ecardPersonAreaHandoverMapper;

    /**
     * 根据网格名称查询网格信息列表
     * @param areaName
     * @param deptId
     * @return
     */
    @Override
    public List<EcardApiPersonArea> selectPersonAreaByAreaName(String areaName, Long deptId, String salt) {
        List<EcardApiPersonArea> personAreaList = ecardPersonAreaMapper.selectPersonAreaByAreaName(areaName, deptId);
        if (personAreaList.size() > 0) {
            for (EcardApiPersonArea personArea : personAreaList) {
                try {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(personArea.getAreaPhone())) {
                        personArea.setAreaPhone(AESEncryptionUtils.decrypt(personArea.getAreaPhone(), salt));
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return personAreaList;
    }

    /**
     * 查询电子名片个人网格区域信息
     * @param id
     * @return
     */
    @Override
    public EcardApiPersonArea selectEcardPersonAreaById(Long id, String salt) {
        EcardApiPersonArea personArea = ecardPersonAreaMapper.selectEcardPersonAreaById(id);
        if (personArea != null) {
            try {
                //数据解密（手机号、身份证号）
                if (!StringUtils.objIsNullOrEmpty(personArea.getAreaPhone())) {
                    personArea.setAreaPhone(AESEncryptionUtils.decrypt(personArea.getAreaPhone(), salt));
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return personArea;
    }

    /**
     * 电子名片-保存个人网格区域信息
     * @param ecardApiPersonArea
     * @return
     */
    @Override
    public int addPersonAreaInfo(EcardApiPersonArea ecardApiPersonArea, String salt) {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardApiPersonArea.getAreaPhone())) {
                ecardApiPersonArea.setAreaPhone(AESEncryptionUtils.encrypt(ecardApiPersonArea.getAreaPhone(), salt));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ecardPersonAreaMapper.addPersonAreaInfo(ecardApiPersonArea);
    }

    /**
     * 条件查询个人网格区域信息列表接口
     * @param ecardApiPersonArea
     * @return
     */
    @Override
    public List<EcardApiPersonArea> getPersonAreaInfoList(EcardApiPersonArea ecardApiPersonArea, String salt) {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardApiPersonArea.getAreaPhone())) {
                ecardApiPersonArea.setAreaPhone(AESEncryptionUtils.encrypt(ecardApiPersonArea.getAreaPhone(), salt));
            }
            List<EcardApiPersonArea> personAreaInfoList = ecardPersonAreaMapper.getPersonAreaInfoList(ecardApiPersonArea);
            if (personAreaInfoList.size() > 0) {
                for (EcardApiPersonArea personArea : personAreaInfoList) {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(personArea.getAreaPhone())) {
                        personArea.setAreaPhone(AESEncryptionUtils.decrypt(personArea.getAreaPhone(), salt));
                    }
                }
            }
            return personAreaInfoList;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 查询个人网格区域信息地图坐标列表接口
     * @param creatorId
     * @param deptId
     * @return
     */
    @Override
    public List<EcardApiPersonArea> getPersonAreaCoordinateList(Long creatorId, Long deptId) {
        return ecardPersonAreaMapper.getPersonAreaCoordinateList(creatorId, deptId);
    }

    /**
     * 根据法人机构ID查询机构下所有网格区域信息坐标信息列表
     * @param deptId
     * @return
     */
    @Override
    public List<EcardApiPersonArea> getAllPersonAreaCoordinateList(Long creatorId, Long deptId) {
        return ecardPersonAreaMapper.getAllPersonAreaCoordinateList(creatorId, deptId);
    }

    /**
     * 网格信息转交
     * @param ecardPersonAreaHandover
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addAreaTransferInfo(EcardApiPersonAreaHandover ecardPersonAreaHandover) {
        //保存转交记录
        ecardPersonAreaHandoverMapper.insertEcardPersonAreaHandover(ecardPersonAreaHandover);
        //更新网格信息的管户人id和机构id
        return ecardPersonAreaMapper.updatePersonAreaUserId(ecardPersonAreaHandover.getAreaId(), ecardPersonAreaHandover.getRecipientId(), ecardPersonAreaHandover.getRecipientDeptId());
    }

    /**
     * 根据ID修改网格信息状态
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int changePersonAreaStatus(Long id) {
        return ecardPersonAreaMapper.changePersonAreaStatus(id);
    }
}
